home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / libdes / enc_writ.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  4KB  |  143 lines

  1. /* lib/des/enc_writ.c */
  2. /* Copyright (C) 1995 Eric Young (eay@mincom.oz.au)
  3.  * All rights reserved.
  4.  * 
  5.  * This file is part of an SSL implementation written
  6.  * by Eric Young (eay@mincom.oz.au).
  7.  * The implementation was written so as to conform with Netscapes SSL
  8.  * specification.  This library and applications are
  9.  * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
  10.  * as long as the following conditions are aheared to.
  11.  * 
  12.  * Copyright remains Eric Young's, and as such any Copyright notices in
  13.  * the code are not to be removed.  If this code is used in a product,
  14.  * Eric Young should be given attribution as the author of the parts used.
  15.  * This can be in the form of a textual message at program startup or
  16.  * in documentation (online or textual) provided with the package.
  17.  * 
  18.  * Redistribution and use in source and binary forms, with or without
  19.  * modification, are permitted provided that the following conditions
  20.  * are met:
  21.  * 1. Redistributions of source code must retain the copyright
  22.  *    notice, this list of conditions and the following disclaimer.
  23.  * 2. Redistributions in binary form must reproduce the above copyright
  24.  *    notice, this list of conditions and the following disclaimer in the
  25.  *    documentation and/or other materials provided with the distribution.
  26.  * 3. All advertising materials mentioning features or use of this software
  27.  *    must display the following acknowledgement:
  28.  *    This product includes software developed by Eric Young (eay@mincom.oz.au)
  29.  * 
  30.  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  31.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  34.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  39.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  40.  * SUCH DAMAGE.
  41.  * 
  42.  * The licence and distribution terms for any publically available version or
  43.  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  44.  * copied and put under another distribution licence
  45.  * [including the GNU Public Licence.]
  46.  */
  47.  
  48. #include <errno.h>
  49. #include <time.h>
  50. #include "des_locl.h"
  51.  
  52. int des_enc_write(fd, buf, len, sched, iv)
  53. int fd;
  54. char *buf;
  55. int len;
  56. des_key_schedule sched;
  57. des_cblock (*iv);
  58.     {
  59. #ifdef _LIBC
  60.     extern int srandom();
  61.     extern unsigned long time();
  62.     extern int random();
  63.     extern int write();
  64. #endif
  65.  
  66.     long rnum;
  67.     int i,j,k,outnum;
  68.     char outbuf[BSIZE+HDRSIZE];
  69.     char shortbuf[8];
  70.     char *p;
  71.     static int start=1;
  72.  
  73.     /* If we are sending less than 8 bytes, the same char will look
  74.      * the same if we don't pad it out with random bytes */
  75.     if (start)
  76.         {
  77.         start=0;
  78.         srandom((unsigned int)time(NULL));
  79.         }
  80.  
  81.     /* lets recurse if we want to send the data in small chunks */
  82.     if (len > MAXWRITE)
  83.         {
  84.         j=0;
  85.         for (i=0; i<len; i+=k)
  86.             {
  87.             k=des_enc_write(fd,&(buf[i]),
  88.                 ((len-i) > MAXWRITE)?MAXWRITE:(len-i),sched,iv);
  89.             if (k < 0)
  90.                 return(k);
  91.             else
  92.                 j+=k;
  93.             }
  94.         return(j);
  95.         }
  96.  
  97.     /* write length first */
  98.     p=outbuf;
  99.     l2n(len,p);
  100.  
  101.     /* pad short strings */
  102.     if (len < 8)
  103.         {
  104.         p=shortbuf;
  105.         memcpy(shortbuf,buf,(unsigned int)len);
  106.         for (i=len; i<8; i++)
  107.             shortbuf[i]=random();
  108.         rnum=8;
  109.         }
  110.     else
  111.         {
  112.         p=buf;
  113.         rnum=((len+7)/8*8); /* round up to nearest eight */
  114.         }
  115.  
  116.     if (des_rw_mode & DES_PCBC_MODE)
  117.         pcbc_encrypt((des_cblock *)p,(des_cblock *)&(outbuf[HDRSIZE]),
  118.             (long)((len<8)?8:len),sched,iv,DES_ENCRYPT); 
  119.     else
  120.         cbc_encrypt((des_cblock *)p,(des_cblock *)&(outbuf[HDRSIZE]),
  121.             (long)((len<8)?8:len),sched,iv,DES_ENCRYPT); 
  122.  
  123.     /* output */
  124.     outnum=rnum+HDRSIZE;
  125.  
  126.     for (j=0; j<outnum; j+=i)
  127.         {
  128.         /* eay 26/08/92 I was not doing writing from where we
  129.          * got upto. */
  130.         i=write(fd,&(outbuf[j]),(unsigned int)(outnum-j));
  131.         if (i == -1)
  132.             {
  133.             if (errno == EINTR)
  134.                 i=0;
  135.             else     /* This is really a bad error - very bad
  136.                  * It will stuff-up both ends. */
  137.                 return(-1);
  138.             }
  139.         }
  140.  
  141.     return(len);
  142.     }
  143.